<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/18 0018
 * Time: 17:32
 */
class AysMysql
{
    public $db_source = '';
    public $db_config = [];
    public function __construct()
    {
        //new swoole_mysql
        $this->db_source = new Swoole\Mysql;

        $this->db_config = [
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'password' => 'root',
            'database' => 'swoole',
            'charset' => 'utf8'
        ];
    }

    public function update(){}

    public function add(){}

    /**
     * mysql 执行逻辑
     * @param $id
     * @param $username
     * @return bool
     */
    public function execute($id, $username)
    {
        //connect
        $this->db_source->connect($this->db_config, function($db, $result) use($id, $username){
            echo "mysql_connect".PHP_EOL;
            //$result 链接返回值
            if($result == false) {
                var_dump($db->connect_error);
            }

            //查询
            //$sql = "select * from test where id=1";
            //更新
            $sql = "update test set `username`='" . $username. "' where `id`= $id";
            //query(add, select, update, delete)
            $db->query($sql, function($db, $result){
                //select  => result 返回的是，查询的结果内容
                //add update delete
                if($result == false){  //失败
                    var_dump($db->error);
                }elseif($result === true){  //add update delete
                    var_dump($db->affected_rows);
                }else{ //select
                    print_r($result);
                }
                $db->close();
            });
        });
        return true;
    }
}
$obj = new AysMysql();
$flag = $obj->execute(1, 'singwa-1');
var_dump($flag).PHP_EOL;
echo 'start'.PHP_EOL;













